
//
// 110517a- AH - Created.
// 110517b- AH - Change from using HashSet to using TreeSet.
// 110517c- AH - getSet().
//

package org.soekrat.engine.forest;

import java.util.Set;
import java.util.TreeSet;

import org.soekrat.engine.util.Util;

/**
 * <p>
 *   {@link TreeVisitor} that get set of all names on
 *   {@link Tree}s.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class GetNamesTreeVisitor implements TreeVisitor 
{
  private final Set<String> m_names=new TreeSet<String>();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public GetNamesTreeVisitor(){}
  
  /**
   * <p>
   *   Clears all collected name.
   * </p>
   */
  public void clear(){ m_names.clear(); }
  
  /**
   * <p>
   *   Gets names collected.
   * </p>
   * @return Names, return the empty array if no names been collected or 
   *         {@link #clear()} been invoked.
   */
  public String[] getNames(){ return m_names.toArray(Util.E_STRING_A); }
  
  /**
   * <p>
   *   Gets the set names has been collected into.
   * </p>
   * <p>
   *   This is a reference to internal set so if visitor is reused content will
   *   change. Take copy of content or use
   *   {@link #getNames()} if to keep result for several traversals.
   * </p>
   * @return Set of collected names.
   */
  public Set<String> getSet(){ return m_names; }

  // Specified in TreeVisitor.
  public void visit(Tree tree){ m_names.add(tree.getName()); }

}
